package com.che.software.testato.web.controller;

import java.io.Serializable;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.che.software.testato.business.HierarchyManager;
import com.che.software.testato.business.IntentionManager;
import com.che.software.testato.business.ItemManager;
import com.che.software.testato.business.IterationManager;
import com.che.software.testato.business.MapDiagramManager;
import com.che.software.testato.business.PrioritizationManager;
import com.che.software.testato.business.ScriptManager;
import com.che.software.testato.domain.entity.Hierarchy;
import com.che.software.testato.domain.entity.search.IntentionSearch;
import com.che.software.testato.domain.entity.search.ItemSearch;
import com.che.software.testato.domain.entity.search.PrioritizationSearch;
import com.che.software.testato.web.controller.acontroller.AbstractController;

/**
 * Controller used to manage the scripts elicitation.
 * 
 * @author Clement HELIOU (clement.heliou@che-software.com).
 * @copyright Che Software.
 * @license GNU General Public License.
 * @see AbstractController, Serializable.
 * @since July, 2011.
 * 
 *        This file is part of Testato.
 * 
 *        Testato is free software: you can redistribute it and/or modify it
 *        under the terms of the GNU General Public License as published by the
 *        Free Software Foundation, either version 3 of the License, or (at your
 *        option) any later version.
 * 
 *        Testato is distributed in the hope that it will be useful, but WITHOUT
 *        ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 *        FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
 *        for more details.
 * 
 *        You should have received a copy of the GNU General Public License
 *        along with Testato. If not, see <http://www.gnu.org/licenses/>.
 * 
 *        Testato's logo is a creation of Arrioch
 *        (http://arrioch.deviantart.com/) and it's distributed under the terms
 *        of the Creative Commons License.
 */
@Component("scriptsElicitationController")
@ManagedBean(name = "scriptsElicitationController")
@Scope("session")
@SessionScoped
public class ScriptsElicitationController extends AbstractController implements Serializable {

	/**
	 * Constants.
	 */
	private static final Logger LOGGER = Logger.getLogger(ScriptsElicitationController.class);
	private static final long serialVersionUID = 7213068536821370814L;

	/**
	 * Members.
	 */
	private Hierarchy currentHierarchy;
	@Autowired
	private transient HierarchyManager hierarchyManager;
	@Autowired
	private transient IntentionManager intentionManager;
	@Autowired
	private transient ItemManager itemManager;
	@Autowired
	private transient IterationManager iterationManager;
	private List<Hierarchy> eligiblesHierarchies;
	@Autowired
	private transient MapDiagramManager mapDiagramManager;
	@Autowired
	private transient PrioritizationManager prioritizationManager;
	@Autowired
	private transient ScriptManager scriptManager;

	/**
	 * Getter for the private field value eligiblesHierarchies.
	 * 
	 * @author Clement HELIOU (clement.heliou@che-software.com).
	 * @return the eligiblesHierarchies field value.
	 * @since July, 2011.
	 */
	public List<Hierarchy> getEligiblesHierarchies() {
		if (null == eligiblesHierarchies) {
			LOGGER.debug("getEligiblesHierarchies(): initialization.");
			try {
				eligiblesHierarchies = hierarchyManager.searchEligiblesHierarchies();
				for (Hierarchy hierarchy : eligiblesHierarchies) {
					if (scriptManager.isScriptExistingFromHierarchyId(hierarchy.getHierarchyId())) {
						hierarchy.setElicited(true);
					}
				}
			} catch (Exception e) {
				LOGGER.error("Error during the eligibles hierarchies search.", e);
			}
		}
		return eligiblesHierarchies;
	}

	/**
	 * Redirection to perform to get the scripts elicitation page.
	 * 
	 * @author Clement HELIOU (clement.heliou@che-software.com).
	 * @return the resulting redirection.
	 * @since August, 2011.
	 */
	public String onAccess() {
		LOGGER.debug("onAccess().");
		eligiblesHierarchies = null;
		return "scriptsElicitation.xhtml?faces-redirect=true";
	}

	/**
	 * Event listener for a hierarchy elicitation.
	 * 
	 * @author Clement HELIOU (clement.heliou@che-software.com).
	 * @return null because none redirection is necessary.
	 * @since July, 2011.
	 */
	public String onElicitateHierarchy() {
		LOGGER.debug("onElicitateHierarchy(" + currentHierarchy.getActionPlanId() + ").");
		try {
			scriptManager.createScriptsFromHierarchy(currentHierarchy.getHierarchyId(), mapDiagramManager.elicitateScriptsFromHierarchy(mapDiagramManager.createMapDiagramFromIntentionsAndItems(intentionManager.searchIntention(new IntentionSearch(currentHierarchy.getActionPlanId())), itemManager.searchItem(new ItemSearch(currentHierarchy.getActionPlanId())))));
			prioritizationManager.createHierarchyPrioritization(currentHierarchy.getHierarchyId());
			iterationManager.createNextIteration(prioritizationManager.searchPrioritization(new PrioritizationSearch(currentHierarchy.getHierarchyId())).get(0), null);
			eligiblesHierarchies = null; // To trigger the data loading.
		} catch (Exception e) {
			LOGGER.error("Error during the scripts elicitation.", e);
		}
		return null;
	}

	/**
	 * Setting a value to the eligiblesHierarchies field.
	 * 
	 * @param eligiblesHierarchies the value to set.
	 */
	public void setEligiblesHierarchies(List<Hierarchy> eligiblesHierarchies) {
		this.eligiblesHierarchies = eligiblesHierarchies;
	}

	/**
	 * Getter for the private field value hierarchyManager.
	 * 
	 * @return the hierarchyManager field value.
	 */
	public HierarchyManager getHierarchyManager() {
		return hierarchyManager;
	}

	/**
	 * Setting a value to the hierarchyManager field.
	 * 
	 * @param hierarchyManager the value to set.
	 */
	public void setHierarchyManager(HierarchyManager hierarchyManager) {
		this.hierarchyManager = hierarchyManager;
	}

	/**
	 * Getter for the private field value mapDiagramManager.
	 * 
	 * @return the mapDiagramManager field value.
	 */
	public MapDiagramManager getMapDiagramManager() {
		return mapDiagramManager;
	}

	/**
	 * Setting a value to the mapDiagramManager field.
	 * 
	 * @param mapDiagramManager the value to set.
	 */
	public void setMapDiagramManager(MapDiagramManager mapDiagramManager) {
		this.mapDiagramManager = mapDiagramManager;
	}

	/**
	 * Getter for the private field value intentionManager.
	 * 
	 * @return the intentionManager field value.
	 */
	public IntentionManager getIntentionManager() {
		return intentionManager;
	}

	/**
	 * Setting a value to the intentionManager field.
	 * 
	 * @param intentionManager the value to set.
	 */
	public void setIntentionManager(IntentionManager intentionManager) {
		this.intentionManager = intentionManager;
	}

	/**
	 * Getter for the private field value itemManager.
	 * 
	 * @return the itemManager field value.
	 */
	public ItemManager getItemManager() {
		return itemManager;
	}

	/**
	 * Setting a value to the itemManager field.
	 * 
	 * @param itemManager the value to set.
	 */
	public void setItemManager(ItemManager itemManager) {
		this.itemManager = itemManager;
	}

	/**
	 * Getter for the private field value currentHierarchy.
	 * 
	 * @return the currentHierarchy field value.
	 */
	public Hierarchy getCurrentHierarchy() {
		return currentHierarchy;
	}

	/**
	 * Setting a value to the currentHierarchy field.
	 * 
	 * @param currentHierarchy the value to set.
	 */
	public void setCurrentHierarchy(Hierarchy currentHierarchy) {
		this.currentHierarchy = currentHierarchy;
	}

	/**
	 * Getter for the private field value scriptManager.
	 * 
	 * @return the scriptManager field value.
	 */
	public ScriptManager getScriptManager() {
		return scriptManager;
	}

	/**
	 * Setting a value to the scriptManager field.
	 * 
	 * @param scriptManager the value to set.
	 */
	public void setScriptManager(ScriptManager scriptManager) {
		this.scriptManager = scriptManager;
	}

	/**
	 * Getter for the private field value prioritizationManager.
	 * 
	 * @return the prioritizationManager field value.
	 */
	public PrioritizationManager getPrioritizationManager() {
		return prioritizationManager;
	}

	/**
	 * Setting a value to the prioritizationManager field.
	 * 
	 * @param prioritizationManager the value to set.
	 */
	public void setPrioritizationManager(PrioritizationManager prioritizationManager) {
		this.prioritizationManager = prioritizationManager;
	}

	/**
	 * Getter for the private field value iterationManager.
	 * 
	 * @return the iterationManager field value.
	 */
	public IterationManager getIterationManager() {
		return iterationManager;
	}

	/**
	 * Setting a value to the iterationManager field.
	 * 
	 * @param iterationManager the value to set.
	 */
	public void setIterationManager(IterationManager iterationManager) {
		this.iterationManager = iterationManager;
	}
}